<template>
  <div>
    <el-table :data="tableData" style="width: 100%">
      <el-table-column label="Id" width="500">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row._id }}</span>
        </template>
      </el-table-column>
      <el-table-column label="姓名" width="300">
        <template slot-scope="scope">
          <el-tag size="medium">{{ scope.row.username }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column>
        <template slot="header" slot-scope="scope">
          <div id="search">
            <el-input
              placeholder="请输入内容"
              v-model="input"
              class="input-with-select"
            >
              <el-select v-model="select" slot="prepend" placeholder="请选择">
                <el-option label="Id" value="id"></el-option>
                <el-option label="姓名" value="username"></el-option>
              </el-select>
              <el-button
                slot="append"
                icon="el-icon-search"
                @click="sousuo"
              ></el-button>
            </el-input>
          </div>
        </template>
        <template slot-scope="scope">
          <el-button
            size="mini"
            @click="
              dialogFormVisible = true;
              xiu(scope.row);
            "
            >修改密码</el-button
          >
          <el-button
            size="mini"
            type="danger"
            v-if="scope.row.state == 1"
            @click="handleDisadle(scope.$index, scope.row)"
            >禁用</el-button
          >
          <el-button
            size="mini"
            type="success"
            v-if="scope.row.state == 2"
            @click="handleLift(scope.$index, scope.row)"
            >解禁</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-dialog title="修改密码" :visible.sync="dialogFormVisible">
      <el-form :model="admin">
        <el-form-item label="管理员id" label-width="200px">
          <span>{{ admin._id }}</span>
        </el-form-item>
        <el-form-item label="管理员姓名" label-width="200px">
          <span>{{ admin.username }}</span>
        </el-form-item>
        <el-form-item label="输入修改后的密码" label-width="200px">
          <el-input v-model="admin.password" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button
          type="primary"
          @click="
            dialogFormVisible = false;
            setAdminPass();
          "
          >确 定</el-button
        >
      </div>
    </el-dialog>
    <div class="block">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[5, 10, 20, 50]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
import apis from "../../http/apis/apis";
export default {
  data() {
    return {
      tableData: [
        {
          _id: 978766534,
          username: "王小虎",
        },
      ],
      dialogFormVisible: false,
      admin: {
        _id: 12454657,
        username: "爱登堡",
        password: "84673",
      },
      select: "",
      input: "",
      currentPage: 1,
      pageSize: 5,
      total: 200,
    };
  },
  mounted() {
    this.getAdmins();
  },
  methods: {
    //获取管理员列表
    async getAdmins() {
      const data = await apis.adminsApi.getAdmins({
        select: this.select,
        input: this.input,
        currentPage: this.currentPage,
        pageSize: this.pageSize,
      });
      console.log("get", data);
      if (data.data.status) {
        this.tableData = data.data.data.data;
        this.total = data.data.data.total;
      }
    },
    //搜索
    sousuo() {
      console.log("sou", this.select, this.input);
      this.getAdmins();
    },
    //获取要修改密码的管理员数据
    xiu(msg) {
      this.admin = msg;
    },
    //确认修改管理员密码
    async setAdminPass() {
      const data = await apis.adminsApi.setAdmins(this.admin);
      this.getAdmins();
    },
    //禁用管理员
    async handleDisadle(index, row) {
      row.state = 2;
      const data = await apis.adminsApi.setAdmins(row);
      this.getAdmins();
    },
    //解禁管理员
    async handleLift(index, row) {
      row.state = 1;
      const data = await apis.adminsApi.setAdmins(row);
      this.getAdmins();
    },
    //每页显示条数
    handleSizeChange(val) {
      this.pageSize = val;
      this.getAdmins();
    },
    //当前页数
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getAdmins();
    },
  },
};
</script>

<style lang="scss" scoped>
#search {
  width: 400px;
  .el-select {
    width: 100px !important;
  }
}
</style>